package net.starwifi.duobao.web.shop;

import net.starwifi.duobao.entity.Area;
import net.starwifi.duobao.entity.Receiver;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.AreaService;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.service.ReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2015/11/9.
 */
@Controller("shopAddressController")
@RequestMapping(value = "/shop")
public class AddressController extends BaseController {

    @Autowired
    AreaService areaService;

    @Autowired
    ReceiverService receiverService;

    @Autowired
    MemberService memberService;

    /**
     * 添加收获地址
     */
    @RequestMapping(value = "/address-add", method = RequestMethod.GET)
    public String addressAdd(Model model,String redirectUrl) {
        final List<Area> areas=  areaService.getCacheListData(" o.parent is null",new Object[]{});
        model.addAttribute("areas",areas);
        model.addAttribute("redirectUrl",redirectUrl);
        return "/shop/address-add";
    }

    /**
     * 添加收获地址提交
     */
    @ResponseBody
    @RequestMapping(value = "/address-save", method = RequestMethod.POST)
    public Message addressSave(Receiver entity,Long district) {
        entity.setIsDefault(Boolean.TRUE);
        entity.setArea(areaService.find(district));
        entity.setMember(memberService.getCurrent());
        receiverService.save(entity);
        return Message.success("地址添加成功");
    }

}
